package ui;

import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.net.InetAddress;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ui.LoginUI.BackgroundPanel;

import listener.SetListener;
import config.SocketConfig;

public class SetUI extends JFrame {
	public JTextField ipAddress;// 定义文本框组件
	public JTextField socketPort,temp;// 定义密码框组件
	JLabel jLabel1, jLabel2;
	JPanel jp1, jp2, jp3;
	public JButton jb1, jb2; // 创建按钮
	// 创建背景面板。
	BackgroundPanel bgp;
	Container ct;

	public SetUI() {
		setResizable(false);
		ct = this.getContentPane();
		this.setLayout(null);
		bgp = new BackgroundPanel(
				(new ImageIcon("images\\background.jpg")).getImage());
		bgp.setBounds(0, 0, 350, 200);
		ct.add(bgp, -1);
		
		temp = new JTextField(6);
		temp.setOpaque(false);
		ipAddress = new JTextField(12);
		socketPort = new JTextField(12);
		ipAddress.setText(SocketConfig.ipAddress);
		socketPort.setText(SocketConfig.socketPort+"");
		jLabel1 = new JLabel("IP地址：");
		jLabel2 = new JLabel("端口号：");
		jb1 = new JButton("确定");
		jb2 = new JButton("取消");
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		
		jp1.setOpaque(false);
		jp2.setOpaque(false);
		jp3.setOpaque(false);
		
		Font fontButton = new Font("SansSerif", Font.BOLD, 15);
		jLabel1.setFont(fontButton);
		jLabel2.setFont(fontButton);
		jb1.setFont(fontButton);
		jb2.setFont(fontButton);

		// 设置布局
		GridLayout gridLayout = new GridLayout(3, 1);

		JPanel jpanelAll = new JPanel();
		jpanelAll.setLayout(gridLayout);
		jpanelAll.setBounds(30, 20, 280, 150);
		jpanelAll.setOpaque(false);

		jp1.add(jLabel1);
		jp1.add(ipAddress);// 第一块面板添加用户名和文本框

		jp2.add(jLabel2);
		jp2.add(socketPort);// 第二块面板添加密码和密码输入框

		jp3.add(temp);
		jp3.add(jb1);
		jp3.add(jb2); // 第三块面板添加确认和取消
		
		jpanelAll.add(jp1);
		jpanelAll.add(jp2);
		jpanelAll.add(jp3);

		SetListener listener = new SetListener(this);
		jb1.addActionListener(listener);
		jb2.addActionListener(listener);
		this.add(jpanelAll,0);
		temp.setEditable(false);
		temp.setEnabled(false);
		temp.setBorder(null);
		// 设置显示
		this.setSize(350, 200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setTitle("系统设置");
	}
	
//	public static void main(String[] args) {
//		new SetUI().setVisible(true);
//	}
	
	/**
	 * 绘制背景图片
	 * 
	 * @author wj
	 * 
	 */
	class BackgroundPanel extends JPanel {
		Image im;

		public BackgroundPanel(Image im) {
			this.im = im;
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(im, 0, 0, this.getWidth(), this.getHeight(), this);
		}

	}

}
